<template>
    <div id="micro-workflow-module-advance__wrapper" class="micro-workflow-module-advance__wrapper">
        <slot />

        <!-- <div v-if="!isWorkflow" class="workflow-micro-opertions-wrapper">
            <g-button-group :btns="btns" />
        </div> -->

        <div v-if="isWorkflow && isLoadPrintSource">
            <h1 style="font-size: 30px; text-align: center; margin-bottom: 20px; margin-top: 20px">
                审批历史
            </h1>
            <ExamineInfo :table-data="currentTaskNodeInfo.examineInfoTableData" />
        </div>
    </div>
</template>

<script lang="ts">
export default {
    name: 'WorkflowModuleAdvance'
}
</script>

<script lang="ts" setup>
import {
    ref,
    watchEffect,
    watch,
    onActivated,
    onDeactivated,
    WatchStopHandle,
    onUnmounted
} from 'vue'
import { BtnProps } from 'jn-ve-global'
import { forParent } from '@jsjn/micro-core-micro-main/sendData'
import { useRoute } from 'vue-router'
import _ from 'lodash'
import { global } from '@jsjn/utils'
import ExamineInfo from './components/examineInfo.vue'
import usePrint from './hooks/usePrint'

const props = withDefaults(
    defineProps<{
        btns?: BtnProps[]
        isSaved?: boolean
        isEdit?: boolean
    }>(),
    {
        btns: () => [],
        isSaved: false,
        isEdit: false
    }
)

const emits = defineEmits(['getSupplementalFormConfig', 'getRouteQuery', 'getCurrentTaskNodeInfo'])
const route = useRoute()
const { isLoadPrintSource, printHandle } = usePrint()
const currentTaskNodeInfo = global['__WORKFLOW_CURRENT_TASK_NODE__']

let savedWatchStop: WatchStopHandle = null
let editWatchStop: WatchStopHandle = null
let btnsWatchStop: WatchStopHandle = null

const isWorkflow = ref<boolean>(global.__IS_WORKFLOW__)

const {
    meta: { isCache: configIsCache },
    query: { isCache: routeQueryIsCache }
} = route

const isCache = configIsCache === true || routeQueryIsCache === 'true'

/**
 * 整个系统的页面缓存 keep-alive 是动态添加的 @jsjn/micro-core-views/cacheAlive
 * 意味着即使是缓存下，首次也不会执行 onActivated，所以要手动调用一次 init
 */
if (isCache) {
    _init()
    onActivated(_init)
    onDeactivated(_deactive)
} else {
    _init()
    onUnmounted(_deactive)
}

function _init() {
    _deactive()

    isWorkflow.value = global.__IS_WORKFLOW__

    if (currentTaskNodeInfo) {
        const info = {
            ...currentTaskNodeInfo,
            from: route.query.from,
            pageType: route.query.type
        }
        emits('getCurrentTaskNodeInfo', info)
    }

    // 抛出路由传递的参数
    if (!_.isEmpty(route.query)) {
        emits('getRouteQuery', route.query)
    }

    /**
     * !这里考虑开发时，获取参数从当前方式获取，即主动触发一次参数抛出
     */
    if (!isWorkflow.value) return

    // 保存完成
    savedWatchStop = watch(
        () => props.isSaved,
        (val) => {
            if (val) {
                forParent({
                    businessNodeIsSaved: val,
                    businessNodeIsEdit: false
                })
            } else {
                forParent({
                    businessNodeIsSaved: val
                })
            }
        },
        { immediate: true }
    )

    // 切换编辑，执行初始化的状态
    editWatchStop = watch(
        () => props.isEdit,
        (val) => {
            forParent({
                businessNodeIsEdit: val
            })
        },
        { immediate: true }
    )

    // 抛出按钮数据
    btnsWatchStop = watchEffect(() => {
        /**
         * 将本地按钮拷贝，抛出，避免基座依靠引用修改本地按钮
         */
        forParent({
            microModuleBtns: props.btns
        })
    })
}

function _deactive() {
    savedWatchStop?.()
    editWatchStop?.()
    btnsWatchStop?.()
}

defineExpose({
    print: printHandle,
    isWorkflow
})
</script>

<style lang="scss" scoped>
.micro-workflow-module-advance__wrapper {
    height: 100%;
    background-color: #fff;
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: auto;
}
</style>
<style></style>
